// 数据渲染
if (sessionStorage.uname) {
    $(".mycar").show()

    // 查找用户表，获取用户id：userid
    $.get("../php/jc_person.php", "hide=getUser", data => {
        console.log(data)
        data.forEach(obj => {
            if (sessionStorage.uman == obj.name) {
                sessionStorage.id = obj.id;
                console.log(sessionStorage.id);
            }
        })
    }, "JSON")

    // 查找购物车页，渲染页面
    $.get("../php/jc_person.php", "hide=getShopcar", data => {
        let total = 0, money = 0;
        data.forEach(obj => {
            if (sessionStorage.uid == obj.userid) {
                $(".mycart-list").append(`<div class='mycart-item'>
                <div class='mycart-item-tb'>
                    <div class='mycart-item-row cartlist clearfix'>
                        <div class='fl sp-1'>
                            <span class='goodscheck input-check' onclick='choose(this)'><i></i></span>
                        </div>
                        <div class='fl sp-2'>
                            <span class='mycart-item-img'>
                                <a href='javascript:void(0);'>
                                    <img src='${obj.img}'>
                                </a>
                            </span>
                        </div>
                        <div class='fl sp-3'>
                            <p class='mycart-item-name'><a href='javascript:void(0);'>${obj.name}</a></p>
                            <div class='mycart-item-tag'>
                                017500
                                <span>限时折扣</span>
                            </div>
                        </div>
                        <div class='fl sp-4'>
                            <div class='mycart-item-sel disabled'>  
                            </div>
                        </div>
                        <div class='fl sp-5'>
                            <div class="mycart-item-per">  
                                <s>${obj.oldprice}元</s>
                                ${obj.newprice}元
                            </div>
                        </div>
                        <div class='fl sp-6'>
                            <div class='mycart-item-count'>
                                <span class='num-minus btn_cart_list_buy_min' onclick='minus(this,${obj.newprice})'>-</span>
                                <input value='${obj.count}' class='inp-count' >
                                <span class='num-plus btn_cart_list_buy_plus' onclick='plus(this,${obj.newprice})'>+</span>
                            </div>
                            <input type='hidden' name=oneid' value=${obj.id} class='oneid'>
                            <p class='mycart-item-dc'>单用户单次限量5个, 请调整购买数量</p>
                        </div>
                        <div class='fl sp-7'>
                            <p class='mycart-item-price'>
                                ${(obj.newprice * obj.count).toFixed(2)}元
                            </p>
                        </div>
                        <div class='fl sp-8'>
                            <div class='mycart-item-tg'>
                                <a class='btn-del' href='javascript:void(0);' onclick='del(this)' id='${obj.id}'>删除</a>
                                <a class='btn-fav' href='javascript:void(0);' onclick='collect(this) id='${obj.id}'>移入收藏夹</a>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div> `);
                total = parseInt(total) + parseInt(obj.count);
                money = money + (obj.count * obj.newprice);
            }
        })
        $(".mycart-tb-info").append(`<p>
                已选商品 
                <b class='total'>${total}</b>
                件&nbsp;&nbsp;总价：
                <b class='money'>${money.toFixed(2)} 元</b>
            </p>
            <p class='b'>不含运费和其他优惠</p>
            <a class='mycart-tb-btn' href='javascript:void(0);'>去结算</a>`)

    }, "JSON")

} else {
    $(".mycar").hide();
}


// 删除
function del(btn) {
    btn = $(btn);
    let id = btn.attr("id");
    console.log(btn.parent().parent().parent().parent().parent())
    $.get("../php/jc_person.php", `hide=delshop&id=${id}&userid=${sessionStorage.uid}`, data => {
        if (data) {
            btn.parent().parent().parent().parent().parent().remove();
        }
    })
}


// +
function plus(btn, price) {
    btn = $(btn);
    count = parseInt(btn.prev().val());
    btn.prev().val(count + 1);
    var total = (count + 1) * price;
    btn.parent().parent().next().children().html(total.toFixed(2) + " 元");

    calcmoney()
    calccount()

    let id = btn.parent().next().val();
    let countnew = btn.prev().val();
    $.get("../php/jc_person.php", `hide=setcount&id=${id}&userid=${sessionStorage.uid}&count=${countnew}`);
}

// -
function minus(btn, price) {
    btn = $(btn);
    count = parseInt(btn.next().val());
    if (btn.next().val() > 1) {
        btn.next().val(count - 1);
        var total = (count - 1) * price;
        btn.parent().parent().next().children().html(total.toFixed(2) + " 元");

        calcmoney()
        calccount()

        let id = btn.parent().next().val();
        let countnew = btn.next().val();
        $.get("../php/jc_person.php", `hide=setcount&id=${id}&userid=${sessionStorage.uid}&count=${countnew}`);
    }
}

function calcmoney() {
    var totalprice = 0;
    var i = -1;
    while (1) {
        i++;
        if ($(".mycart-item-price")[i]) {
            totalprice += parseInt($(".mycart-item-price")[i].innerHTML)
        } else {
            $(".money").html(totalprice.toFixed(2) + " 元")
            return false;
        }

    }
}

function calccount() {
    let countall = 0;
    let i = -1;
    while (1) {
        i++;
        if ($(".inp-count")[i]) {
            countall += parseInt($(".inp-count")[i].value)
        } else {
            $(".total").html(countall);
            $(".shopcar-sum").html(countall);
            return false;
        }
    }
}


// 全选
$(".cartcheck ").click(e => {
    e = window.event;
    target = $(e.currentTarget)
    target.toggleClass("checked");
    let goods=target.parent().next().children().children();
    if(target.attr("class")=="input-check cartcheck checked"){
        for(var i=0;i<goods.length;i++){
            $($($($(goods[i]).children()[0]).children()[0]).children()[0]).addClass("checked")
        }
    }else if(target.attr("class")=="input-check cartcheck"){
        for(var i=0;i<goods.length;i++){
            $($($($(goods[i]).children()[0]).children()[0]).children()[0]).removeClass("checked")
        }
    }
    
    
})

// 单个商品选择
function choose(btn) {
    btn = $(btn);
    btn.toggleClass("checked");
}